package com.ruoyi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.mapper.HouseMapper;
import com.ruoyi.mapper.RPMapper;
import com.ruoyi.service.RoomStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoomStatusServiceImpl implements RoomStatusService {

    @Autowired
    HouseMapper houseMapper;

    @Autowired
    RPMapper rpMapper;

    @Override
    public AjaxResult getRoomStatusList(String hotelId, List<String> columnDateRange) {
        // step1:找到这个酒店下的所有房型
        Map getHouseListMap = new HashMap();
        getHouseListMap.put("hotelId", hotelId);
        getHouseListMap.put("isOnline", 1);
        List<Map> houseList = houseMapper.getHouseListByHotelId(getHouseListMap);
        List<Map> mapList = new ArrayList<>();

        String startTime = columnDateRange.get(0);
        String endTime = columnDateRange.get(columnDateRange.size() - 1);

        List<Map> dayList = new ArrayList<>();

        for (String nextNDay : columnDateRange) {
            Map map = new HashMap();
            map.put("filedAsName", nextNDay.replace("-", "_"));
            map.put("day", nextNDay);
            dayList.add(map);
        }

        // 循环房型
        for (Map map : houseList) {
            String houseId = map.get("houseId").toString();
            // step2 找房型维度数据
            Map map1 = new HashMap();
            map1.put("houseId", houseId);
            map1.put("startTime", startTime);
            map1.put("endTime", endTime);
            map1.put("dayList", dayList);
            Map houseDayData = houseMapper.getHouseDayDataByHouseIdAndTime(map1);
            if (houseDayData == null) {
                // 说明没该时间区间没设置采购价
                houseDayData = houseMapper.getHouseObjByObj(map1);
                // 告诉前端这个时间区间没设置采购价
                houseDayData.put("noRpPrice", "1");
            }
            houseDayData.put("type", "1");
            mapList.add(houseDayData);

            // step3找该房型下的所有产品
            Map getRPMap = new HashMap();
            getRPMap.put("houseId", houseId);
            getRPMap.put("isOnline", "1");
            List<Map> rpList = rpMapper.getRPByHouseId(getRPMap);
            for (Map map2 : rpList) {
                String rpId = map2.get("rpId").toString();
                Map map3 = new HashMap();
                map3.put("rpId", rpId);
                map3.put("startTime", startTime);
                map3.put("endTime", endTime);
                map3.put("dayList", dayList);
                // step4 找产品维度数据
                Map rpDayData = rpMapper.getRPDayDataByRPIdAndTime(map3);
                if (rpDayData == null) {
                    // 说明没该时间区间没设置采购价
                    rpDayData = rpMapper.getRpObjByRpId(rpId);
                    // 告诉前端这个时间区间没设置采购价
                    rpDayData.put("noRpPrice", "1");
                }
                rpDayData.put("type", "2");
                mapList.add(rpDayData);
            }
        }
        return AjaxResult.success(mapList);
    }
}
